<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠<azthink@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;

/**
 * 配置模型
 *
 * @author 李忠<azthink@qq.com>
 * @createAt 2018-08-31 10:29:59
 */
class Config extends Model {

    // 设置当前模型对应的完整数据表名称
    protected $table = '__SYS_CONFIG__';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    protected function getTypeTextAttr($value, $data) {
        return get_config_text('config_type_list', $data['type']);
    }

    protected function getGroupTextAttr($value, $data) {
        if ($data['group'] != 0) {
            return get_config_text('config_group_list', $data['group']);
        } else {
            return '无';
        }
    }

    // 修改器
    public function getStatusTextAttr($value, $data) {
        return get_status_html($data['status']);
    }

    // 自定义方法
    public static function lists($title, $group, $type) {
        $map = array();
        if ($group !== '' && !empty($group)) {
            array_push($map, ['a.group', '=', $group]);
        }
        if ($type !== '' && !empty($type)) {
            array_push($map, ['a.type', '=', $type]);
        }
        array_push($map, ['a.status', '=', 1]);

        if ($title !== '' && !empty($title)) {
            array_push($map, ['a.title|a.name', 'like', '%' . $title . '%']);
        }
        return Config::alias('a')->where($map)->paginate();
    }

    public static function state($id, $status) {
        return self::update(['status' => $status], ['id' => $id]);
    }

    /**
     * 获取数据库中的配置列表
     *
     * @return array 配置数组
     */
    public static function listConfig() {
        $map = array('status' => 1);
        $data = Config::where($map)->field('type,name,value')->select();

        $config = array();
        if ($data) {
            foreach ($data as $value) {
                $config[$value['name']] = self::parse($value['type'], $value['value']);
            }
        }
        return $config;
    }

    public static function listByGroup($group) {
        $map = array(['status', '=', 1], ['group', '=', $group]);
        $data_list = Config::where($map)->order('sort')->select();

        /* $config_list = array();
        if ($data_list) {
            foreach ($data_list as $value) {
                $config_list[$value['name']] = self::parse($value['type'], $value['value']);
            }
        } */
        return $data_list;

    }

    /**
     * 根据配置类型解析配置
     *
     * @param integer $type
     *            配置类型
     * @param string $value
     *            配置值
     */
    private static function parse($type, $value) {
        switch ($type) {
            case 3: // 解析数组
                $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
                if (strpos($value, ':')) {
                    $value = array();
                    foreach ($array as $val) {
                        list ($k, $v) = explode(':', $val);
                        $value[$k] = $v;
                    }
                } else {
                    $value = $array;
                }
                break;
        }
        return $value;
    }
}
